﻿using System.Data.SqlClient;
using System.IO;
using Loki.Data.SqlServer;
using NUnit.Framework;

namespace Luna.Data.SqlServer.Test
{
    public class SqlManagerTest : AssertionHelper
    {
        private const string NOT_FOUND = "xxx.mdf";
        private const string TEST_FILE = "Test.mdf";
        private const string TEST_FILE_2 = "Test2.mdf";
        private const string TEST_log_FILE = "Test_log.ldf";
        private const string TEST_log_FILE_2 = "Test2_log.ldf";
        private LocalSqlDatabaseManager _Manager;

        [SetUp]
        public void SetUp()
        {
            if (File.Exists(TEST_FILE))
            {
                File.Delete(TEST_FILE);
            }

            if (File.Exists(TEST_log_FILE))
            {
                File.Delete(TEST_log_FILE);
            }

            if (File.Exists(TEST_FILE_2))
            {
                File.Delete(TEST_FILE_2);
            }

            if (File.Exists(TEST_log_FILE_2))
            {
                File.Delete(TEST_log_FILE_2);
            }
            _Manager = new LocalSqlDatabaseManager();
            _Manager.Current = new SqlDatabaseAdapter();
        }

        [TearDown]
        public void TearDown()
        {
            _Manager.Dispose();
            /* if (File.Exists(TEST_FILE))
             {
                 File.Delete(TEST_FILE);
             }

             if (File.Exists(TEST_log_FILE))
             {
                 File.Delete(TEST_log_FILE);
             }

             if (File.Exists(TEST_FILE_2))
             {
                 File.Delete(TEST_FILE_2);
             }

             if (File.Exists(TEST_log_FILE_2))
             {
                 File.Delete(TEST_log_FILE_2);
             }*/
        }

        [Test]
        public void TestCreate()
        {
            _Manager.Create(TEST_FILE);
            Expect(File.Exists(TEST_FILE));
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(_Manager.Current.ConnectionString);
            Expect(builder.AttachDBFilename == Path.GetFullPath(TEST_FILE));
        }

        [Test]
        public void TestOpen()
        {
            _Manager.Create(TEST_FILE);
            _Manager.Open(TEST_FILE);
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(_Manager.Current.ConnectionString);
            Expect(builder.AttachDBFilename == Path.GetFullPath(TEST_FILE));
        }

        [Test]
        public void TestOpenFailed()
        {
            Expect(() => _Manager.Open(NOT_FOUND), Throws.Exception);
        }

        [Test]
        public void TestSaveAs()
        {
            _Manager.Create(TEST_FILE);
            Expect(_Manager.Current.DataSourceDescription, Is.Not.Empty);
            _Manager.SaveAs(TEST_FILE_2);
            Expect(File.Exists(TEST_FILE_2));
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(_Manager.Current.ConnectionString);
            Expect(builder.AttachDBFilename == Path.GetFullPath(TEST_FILE_2));
        }

        [Test]
        public void TestSaveFailed()
        {
            Expect(() => _Manager.SaveAs(Path.GetTempFileName()), Throws.Exception);
        }
    }
}